﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dhsystem.Main;
using dhSystemBLL.orderData;
using baseSet.importExport;
using dhDataAcess.dataAcess;
using dhDataAcess.dataAcess.orderDataSetTableAdapters;
using baseSet.power;

namespace dhsystem.orderForm
{
    public partial class FrmCustList : Form
    {
        custListBLL cust = new custListBLL();
        custAddListBLL custAddList = new custAddListBLL();
        string state;
        string workerNum;
        public FrmCustList()
        {
            InitializeComponent();
        }

        //新增按钮，显示新增窗口
        private void toolAdd_Click(object sender, EventArgs e)
        {
            state = "新增";
            FrmCustListAdd custListAdd = new FrmCustListAdd(bindingSourceCustList , state );
            custListAdd.ShowDialog();
        }
        //打开窗口时显示列表数据
        private void FrmCustList_Load(object sender, EventArgs e)
        {

            getData();

        }
        //工具栏查询按钮，查询所有列表数据
        private void toolQuery_Click(object sender, EventArgs e)
        {

            FrmDataQuery query = new FrmDataQuery(moduleInfo.custList );
            DialogResult result = query.ShowDialog();
            if (result == DialogResult.Yes)
            {
                dGridCustList.DataSource = query.rTable;
            }

        }
        //工具栏的编辑按钮,修改数据
        private void toolEdit_Click(object sender, EventArgs e)
        {
            workerNum = dGridCustList.Rows[dGridCustList.CurrentRow.Index].Cells[10].Value.ToString();
            state = "编辑";
            FrmCustListAdd custListAdd = new FrmCustListAdd(bindingSourceCustList, state,workerNum );
            custListAdd.ShowDialog();
        }
        //删除按钮，删除选中行数据
        private void toolDel_Click(object sender, EventArgs e)
        {
            string custNum = dGridCustList.Rows[dGridCustList.CurrentRow.Index].Cells[1].Value.ToString();
            try
            {
                DialogResult result = MessageBox.Show("是否删除客户编号为：" + custNum + "的客户！", "提示信息", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {

                    bindingSourceCustList.DataSource = cust.custListDel(custNum);
                    dGridCustList.DataSource = bindingSourceCustList;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            bindingSourceCustList.DataSource = cust.getcustListAll();
        }
        //查询按钮，按条件查询数据
        private void buttonQuery_Click(object sender, EventArgs e)
        {
            try
            {
                bindingSourceCustList .DataSource =cust .getcustListAll ("%" + textcustNameQuery .Text  + "%");
                dGridCustList.DataSource = bindingSourceCustList;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        //刷新按钮
        private void buttupdate_Click(object sender, EventArgs e)
        {
            getData();
        }
        //工具栏退出按钮，退出窗口
        private void toolExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //导入按钮
        private void buttImport_Click(object sender, EventArgs e)
        {
            FrmImport import = new FrmImport(moduleInfo.custList );
            import.ShowDialog();
        }
        //datagridview第一列显示行号
        //private void dGridCustList_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        //{
        //    //e.Row.HeaderCell.Value = string.Format("{0}", e.Row.Index + 1);
        //}

        //private void dGridCustList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        //{
        //    baseSet.datGridSet.gridViesSet.setGridViewRowNum(e, dGridCustList);
        //}
        private void getData()
        {
            custAddListTableAdapter custAddListAdapter = new custAddListTableAdapter();
            custListTableAdapter custListAdapter = new custListTableAdapter();
            orderDataSet cust = new orderDataSet();
            custAddListAdapter.FillcustAddListAll (cust.custAddList);
            custListAdapter.FillcustListAll (cust.custList);
           bindingSourceCustList .DataSource  = cust;
           bindingSourceCustList.DataMember = "custList";
           dGridCustList .DataSource  = bindingSourceCustList ;

            DcolumnSet.colSet(moduleInfo.custList , dGridCustList );
            toolStripStatusLabel1.Text = "客户表数据总条数:" + dGridCustList.RowCount.ToString() + "条";
            bindingSourcecustAddList.DataSource = bindingSourceCustList ;

            bindingSourcecustAddList.DataMember = "FK_custAddList_custList";
           dGridCustListadd .DataSource  = bindingSourcecustAddList;
           DcolumnSet.colSet(moduleInfo.custAddList , dGridCustListadd );
           toolStripStatusLabel3.Text = "客户地址表数据总条数:" + dGridCustListadd.RowCount.ToString() + "条";

           sysDataSet.modulAaccessDataTable modulAaccess = powerSet.modulPower(moduleInfo.custList );
           foreach (sysDataSet.modulAaccessRow modulAaccessRow in modulAaccess)
           {
               buttonQuery.Enabled = modulAaccessRow.showAc;
               toolAdd.Enabled = modulAaccessRow.addAc;
               toolEdit.Enabled = modulAaccessRow.editAc;
               toolDel.Enabled = modulAaccessRow.delAc;
               toolPrint.Enabled = modulAaccessRow.printAc;
               toolExport.Enabled = modulAaccessRow.exportAc;
               buttImport.Enabled = modulAaccessRow.importAc;

           }
        }

        private void toolExport_Click(object sender, EventArgs e)
        {
            FrmExcelExport frm = new FrmExcelExport(moduleInfo.custList , dGridCustList , "客户信息");
            frm.ShowDialog();
        }

        private void 添加送货地址ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bindingSource1.DataSource = custAddList.getcustAddListAll();
            string custNum = dGridCustList.CurrentRow.Cells["custNum"].Value.ToString();
            FrmCustAddAdd custAdd = new FrmCustAddAdd(bindingSource1 ,"新增",custNum );
            custAdd.ShowDialog();
        }

        private void dGridCustList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            baseSet.datGridSet.gridViesSet.setGridViewRowNum(e, dGridCustList);
        }
    }
}
